#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("unTitle");
    connect(ui->newAction,SIGNAL(triggered()),this,SLOT(newFileSlot()));
    connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
    connect(ui->saveAsAction,SIGNAL(triggered()),this,SLOT(saveAsFileSlot()));
    connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
    connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close()));
    //edit menu
    connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));
    connect(ui->redoAction,SIGNAL(triggered()),ui->textEdit,SLOT(redo()));
    connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));
    connect(ui->copyAction,SIGNAL(triggered()),ui->textEdit,SLOT(copy()));
    connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
    connect(ui->selectAllAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));
    connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
    connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));
    connect(ui->aboutAction,SIGNAL(triggered()),this,SLOT(aboutSoftwareSlot()));
}

MainWindow::~MainWindow()
{
    delete ui; //垃圾回收机制，回收那些没被dielete的ui控件，在ui界面拖入的对象就相当于new了
    //对qtwidget有效，qtcore等模块无效?
}

void MainWindow::newFileSlot() {
    qDebug() <<"11";
    if (ui->textEdit->document()->isModified())
    {
        qDebug() <<"md";
    }
    else
    {
        ui->textEdit->clear();
        this->setWindowTitle("unTitle--gyf");
        qDebug() <<"nmd";
    }
}

void MainWindow::openFileSlot() {
    QString fileName = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());
    qDebug() << fileName;
    if (fileName.isEmpty()) {
        QMessageBox::information(this,"Error Message","Please Select A Text File");
    }
    else
    {
        QFile *file = new QFile;
        file -> setFileName(fileName);
        bool ok = file->open(QIODevice::ReadOnly);
        if (ok) {
            QTextStream in(file);
            ui->textEdit->setText(in.readAll());
            file -> close();
            delete file;
        } else {
            QMessageBox::information(this,"Error Message","File open error");
        }
    }
}

void MainWindow::saveFileSlot() { //另存为这么写还有问题，后期再说
    if (saveFileName.isEmpty()) {
        this->saveAsFileSlot();
    }
    else
    {
        QFile *file = new QFile;
        file -> setFileName(saveFileName);
        bool ok = file->open(QIODevice::WriteOnly);
        if (ok) {
            QTextStream out(file);
            out<<ui->textEdit->toPlainText(); //去除textEdit中的纯文本
            file -> close();
            delete file;
        }
    }
}

void MainWindow::saveAsFileSlot() {
    QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath());
    if (saveFileName.isEmpty()) {
        QMessageBox::information(this,"Error Message","Please Select A File");
    }
    else
    {
        QFile *file = new QFile;
        file -> setFileName(saveFileName);
        bool ok = file->open(QIODevice::WriteOnly);
        if (ok) {
            QTextStream out(file);
            out<<ui->textEdit->toPlainText(); //去除textEdit中的纯文本
            this->setWindowTitle(saveFileName);
            file -> close();
            delete file;
        } else {
            QMessageBox::information(this,"Error Message","Save File Error");
        }
    }
}

//打印看QPrintDialog

void MainWindow::aboutSoftwareSlot() {

    about *dialog = new about; //unmodal dialog
    dialog->exec();//modal dialog
//    dialog->show();
}

void MainWindow::closeEvent(QCloseEvent *event) {
    if (ui->textEdit->document()->isModified()) {

        QMessageBox msgBox;
        msgBox.setText("文件已被变更");
        msgBox.setInformativeText("您是否要保存文件");
        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Save);

        int ret = msgBox.exec();
        switch (ret) {
        case QMessageBox::Save:
            // Save was clicked
            this->saveFileSlot();
            break;
        case QMessageBox::Discard:
            // Don't Save was clicked
            event->accept();
            break;
        case QMessageBox::Cancel:
            // Cancel was clicked
            event->ignore();
            break;
        default:
            // should never be reached
            break;
        }
    }
    else
    {

    }
//    event->ignore();
    event->accept();

}
